package epam.marchenko.library.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;




import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import epam.marchenko.library.domain.BookClass;

@Repository(value="bookClassRepository")
public class BookClassRepositoryJpaImpl implements BookClassRepository {
	@PersistenceContext
	private EntityManager em;

	public BookClassRepositoryJpaImpl() {}

	public BookClassRepositoryJpaImpl(EntityManager em) {
		this.em = em;
	}

	public BookClass findById(int id) {
		return em.find(BookClass.class, id);
	}

	public BookClass find(BookClass bookClass) {
		return em.find(BookClass.class, bookClass.getId());
	}

	public List<BookClass> findAll() {
		TypedQuery<BookClass> query = em.createNamedQuery("BookClass.findAll",
				BookClass.class);
		return query.getResultList(); 
	}

	@Transactional
	public void create(BookClass bookClass) {
		  em.persist(bookClass);
	}

	@Transactional
	public void delete(BookClass bookClass) {
			  em.remove(findById(bookClass.getId()));
	}

	@Transactional
	public void deleteById(int id) {
			  em.remove(findById(id));
	}

	@Transactional
	public void update(BookClass bookClass) {
			  em.merge(bookClass);
	}

}
